
class OsnMember {
    func fromJson(_ json:[String:Any], _ member:DMCCMember?) ->DMCCMember {
        let m = DMCCMember()
        m.groupId = json["groupID"] as? String ?? ""
        m.memberId = json["osnID"] as? String ?? ""
        m.alias = json["nickName"] as? String ?? ""
        m.mute = json["mute"] as? Int ?? 0
        m.type = OsnMemberType(rawValue: json["type"] as? Int ?? 0)
        if let member = member {
            if m.alias.isEmpty {m.alias = member.alias}
            if m.mute == 0 { m.mute = member.mute}
            if m.type == OsnMemberType.Normal { m.type = member.type}
        }
        return m
    }
    func fetchMemberZone(_ groupId:String, _ begin:Int, _ count:Int) async ->MemberZoneInfo? {
        let data:[String:Any] = ["begin": begin, "size": count]
        guard let json = await gimService.imRequest("GetMemberZone", groupId, data) else {return nil}
        guard let res = gimService.getResult(json) else {return nil}
        return MemberZoneInfo.fromJson(res)
    }
    func getMemberInfo(_ groupId:String, _ memberId:String) ->DMCCMember? {
        return dbMember.getMemberInfo(groupId, memberId)
    }
    func getMemberInfoDefault(_ groupId:String, _ memberId:String) ->DMCCMember {
        if let member = getMemberInfo(groupId, memberId){
            member.isNull = false
            return member
        }
        let member = DMCCMember()
        member.groupId = groupId
        member.memberId = memberId
        member.isNull = true
        Task { await gidService.refreshId(IdsType.user, memberId) }
        return member
    }
    func getMemberList(_ groupId:String, _ begin:Int, _ count:Int) ->[DMCCMember] {
        return dbMember.listMemberWithIndex(groupId, begin, count)
    }
    func setMemberInfo(_ info:DMCCMember) ->Bool {
        return dbMember.addMemberInfo(info)
    }
    func clearMemberInfo(_ groupId:String) ->Bool {
        return dbMember.clearMemberInfo(groupId)
    }
    func delMemberInfo(_ groupId:String, _ osnId:String) ->Bool {
        return dbMember.delMemberInfo(groupId, osnId)
    }
    func acceptMember(_ userId:String, _ groupId:String) async ->Bool {
        let data:[String:Any] = ["userID": userId]
        return await gimService.imRequest("AgreeMember", groupId, data) != nil
    }
    func addInitMembers(_ groupId:String, _ members:[String]){
        for m in members{
            let member = DMCCMember()
            member.groupId = groupId
            member.memberId = m
            _ = setMemberInfo(member)
        }
    }
    func updateMemberZone(_ groupId:String, _ begin:Int, _ count:Int) async->[DMCCMember] {
        var ms:[DMCCMember] = []
        if let zone = await fetchMemberZone(groupId, begin, count){
            for member in zone.userList {
                let m = getMemberInfoDefault(groupId, member.osnID)
//                m.mute = member.mute
                m.type = OsnMemberType(rawValue: member.type)
                _ = setMemberInfo(m)
                ms.append(m)
            }
        }
        return ms
    }
    func setMembersMute(_ groupId:String, _ members:[String], _ mute:Bool){
        for m in members {
            if let info = getMemberInfo(groupId, m) {
                info.mute = if mute {1}else{0}
                let res = setMemberInfo(info)
                if res {
                    
                }
            }
        }
    }
    func modifyMemberInfo(_ groupId:String, _ memberInfo:[String:Any]) async ->Bool {
        return await gimService.imRequestWithError("SetMemberInfo", groupId, memberInfo).1 == nil
    }
    func setMemberAlias(_ groupId:String, _ alias:String) async {
        _ = await modifyMemberInfo(groupId, [ "nickName": alias ])
        let info = getMemberInfoDefault(groupId, gimService.mOsnId)
        info.alias = alias
        _ = setMemberInfo(info)
    }
}
let gosnMember = OsnMember()
